/**
 * Created by duwei on 2019/2/15.
 */
var mCard = {
    init: function () {
    },
    fontType: {
        0: 'common',
        1: 'deafult',
        2: 'zipai0'
    },
    fontDate: {
        'deafult': {
            originalSize: {w: 84, h: 104},
            cardSize: {w: 84, h: 104},
            cardstr: 'card',
            cardScale: 1,
            prefix: '',
            bigSize: {w: 76, h: 226},
            smallSize: {w: 42, h: 42},
            small2Size: {w: 34, h: 34},
            backSize: {w:42, h:42},
            touchRect: cc.rect(0, 25, 84, 104),
            offsetY: 25,
        },
        'common': {
            originalSize: {w: 98, h: 122},
            cardSize: {w: 98, h: 122},
            cardstr: 'card_common',
            cardScale: 1,
            prefix: 'cardcommon_',
            bigSize: {w: 76, h: 226},
            smallSize: {w: 42, h: 42},
            small2Size: {w: 34, h: 34},
            backSize: {w:42, h:42},
            touchRect: cc.rect(0, 25, 98, 122),
            offsetY: 25,
        },
        'zipai0': {
            originalSize: {w: 95, h: 137},
            cardSize: {w: 95, h: 117},
            cardstr: 'zipai0',
            cardScale: 1,
            prefix: 'zipai0_',
            bigSize: {w: 76, h: 226},
            smallSize: {w: 42, h: 42},
            small2Size: {w: 34, h: 34},
            backSize: {w:42, h:42},
            touchRect: cc.rect(0, 45, 95, 117),
            offsetY: 40,
        }
    },
    /**
     * go语言
     */
    comboTypes: {
        peng: 1,
        kan: 2,
        wei: 3,
        chouwei: 4,
        pao: 5,
        ti: 6,
        o123: 7,
        o2710: 8,
        o234: 9,
        dui: 10,
        jiao: 11,
        none: 12,
        chi: 13,
        hu: 14,
        chongpao: 15,
        xiahuo: 16,
        wangdiao: 21,
        wangchuang: 22,
        wangzha: 23
    },
    comboTypes_ZN: {
        '碰': 1,
        '坎': 2,
        '偎': 3,
        '臭偎': 4,
        '跑': 5,
        '提': 6,
        '一二三': 7,
        '二七十': 8,
        '一句话': 9,
        '对': 10,
        '绞': 11,
        none: 12,
        chi: 13,
        hu: 14
    },
    //1-12
    comboName: ['碰', '坎', '偎', '偎', '跑', '提', '吃', '吃', '吃', '绞', '吃', ''],
    comboImg: ['peng', 'kan', 'wei', 'wei', 'pao', 'ti', 'chi', 'chi', 'chi', 'jiang', 'chi', 'none'],
    combo_hx_Big: [3, 6, 6, 6, 9, 12, 6, 6, 0, 0, 0, 0, 0, 0],
    combo_hx_Small: [1, 3, 3, 3, 6, 9, 3, 3, 0, 0, 0, 0, 0, 0],
    /**
     * java语言
     */
    duiType: {
        0: 'none',
        1: 'dui',
        2: 'chi',
        3: 'kan',
        4: 'peng',
        5: 'wei',
        6: 'chouwei',
        7: 'ti',
        8: 'pao'
    },
    getCardFontType: function () {
        return settingData.hz_pai || 0;
    },
    /**
     * 牌的资源名称
     * @param cardname
     * @returns {*}
     */
    getCardImg: function (cardname) {
        if (cardname.indexOf('NaN') >= 0) return 'backbig.png';
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        var src = mCard.fontDate[fontType].prefix + cardname + '.png';
        return src;
    },
    /**
     * 牌的偏移Y
     */
    getCardOffsetY: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].offsetY;
    },
    /**
     * 牌的大小
     * @returns {mCard.fontDate.deafult.cardSize|{w, h}|mCard.fontDate.common.cardSize|*}
     */
    getCardSize: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].cardSize;
    },
    /**
     * 牌的描述
     * @returns {string|string}
     */
    getCardstr: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].cardstr;
    },
    /**
     * 小牌尺寸
     */
    getSmallSize: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].smallSize;
    },
    /**
     * 小牌尺寸 * 80%
     */
    getSmall2Size: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].small2Size;
    },
    /**
     * 被牌牌尺寸 42*42
     */
    getBackSize: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].backSize;
    },
    /**
     * 触摸区域
     */
    getouchRect: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].touchRect;
    },
    /**
     * 手牌原始尺寸
     */
    getOriginalSize: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].originalSize;
    },
    /**
     * 大牌尺寸
     */
    getBigSize: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].bigSize;
    },
    /**
     * 牌的缩放
     * @returns {number|*}
     */
    getCardScale: function () {
        var hz_pai = settingData.hz_pai;
        var fontType = mCard.fontType[hz_pai] || mCard.fontType[0];
        return mCard.fontDate[fontType].cardScale;
    },
    /**
     * 拷贝手牌
     * @param list
     * @returns {string|*|WordArray|Array.<T>}
     */
    copyCards: function (list) {
        var newCards = list.concat([]);
        return newCards;
    },
    /**
     * 拷贝组合
     * @param list
     * @returns {Array}
     */
    copyComboList: function (list) {
        var newList = [];
        if (list != null) {
            for (var i = 0; i < list.length; i++) {
                var comboInfo = list[i];

                var newCombo = {};
                newCombo.typ = comboInfo.typ;
                newCombo.cards = comboInfo.cards.concat([]);
                newList.push(newCombo);
            }
        }
        return newList;
    },
    getJavaComboType: function (type) {
        var typ = mCard.comboTypes[mCard.duiType[type]];
        return typ;
    },
    /**
     * 组合类型
     * @param combo
     * @param ziMo
     * @returns {number}
     */
    getComboType: function (combo, ziMo) {
        var typ = mCard.comboTypes.none;
        combo.sort(function (a, b) {
            return a - b;
        });

        if (combo.length == 4) {
            // var c1= parseInt(combo[0]);
            // var c2= parseInt(combo[1]);
            // var c3= parseInt(combo[2]);
            // var c4= parseInt(combo[3]);
            // if(ziMo == true)
            //     if(c1 == c2 && c1 == c3 && c1 == c4){
            //         typ = mCard.comboTypes.ti;
            //     }
            // else
            //     typ = mCard.comboTypes.pao;
        } else if (combo.length == 3) {
            var c1 = parseInt(combo[0]);
            var c2 = parseInt(combo[1]);
            var c3 = parseInt(combo[2]);
            var is1510 = ZiPaiRoomData.options['o1510'];

            if (c1 == c2 && c1 == c3) {
                if (ziMo == true)
                    typ = mCard.comboTypes.wei;
                else
                    typ = mCard.comboTypes.peng;
            } else if (c1 == c2 - 1 && c2 == c3 - 1) {
                if (c1 == 1 || c1 == 11) {
                    typ = mCard.comboTypes.o123;
                } else {
                    typ = mCard.comboTypes.o234;
                }
            } else if (c1 == 2 && c2 == 7 && c3 == 10) {
                typ = mCard.comboTypes.o2710;
            } else if (c1 == 12 && c2 == 17 && c3 == 20) {
                typ = mCard.comboTypes.o2710;
            } else if (is1510 == true && c1 == 1 && c2 == 5 && c3 == 10) {
                typ = mCard.comboTypes.o2710;
            } else if (is1510 == true && c1 == 11 && c2 == 15 && c3 == 20) {
                typ = mCard.comboTypes.o2710;
            } else {
                typ = mCard.comboTypes.jiao;
            }
        } else if (combo.length == 2) {
            typ = mCard.comboTypes.dui;
        }
        return typ;
    },
    //显示手牌里的胡息
    getHandsHuXi: function (hands) {
        var handhuxi = 0;
        for (var i = 0; i < hands.length; i++) {
            var typ = mCard.getComboType(hands[i], true);
            var huxi = mCard.getComboHuXi({cards: hands[i], typ: typ});
            // console.log(huxi + "===" + typ);
            handhuxi += huxi;
        }
        return handhuxi;
    },
    /**
     * 显示的胡息  自己的话计算全部   不是自己不计算偎
     */
    getHuXi: function (comboList, isSelf) {
        var total = 0;
        for (var i = 0; i < comboList.length; i++) {
            var comboInfo = comboList[i];

            var xi = 0;
            var typ = comboInfo.typ;
            //偎不计算胡息
            if (isSelf == null || isSelf == undefined || isSelf == true) {
                if (comboInfo.cards[0] > 10) {
                    xi = mCard.combo_hx_Big[typ - 1];
                } else {
                    xi = mCard.combo_hx_Small[typ - 1];
                }
                total += xi;
            } else {
                if (typ != mCard.comboTypes.wei) {
                    if (comboInfo.cards[0] > 10) {
                        xi = mCard.combo_hx_Big[typ - 1];
                    } else {
                        xi = mCard.combo_hx_Small[typ - 1];
                    }
                    total += xi;
                }
            }
        }
        return total;
    },
    /**
     * 偎麻雀的胡息
     * 碰   2 3
     * 偎   3 4
     * 将   0 2
     * 团圆 碰(偎)+一句话  3 4
     * 转弯 将+一句话     2 3
     * @param comboList
     * @returns {number}
     */
    getHuXiWeiMaQue: function (comboList) {
        var total = 0;
        var cardNumArray = [];
        for (var i = 0; i < comboList.length; i++) {
            var comboInfo = comboList[i];
            var xi = 0;
            var typ = comboInfo.typ;
            if (typ == mCard.comboTypes.peng) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    total += 3;
                } else {
                    total += 2;
                }
            } else if (typ == mCard.comboTypes.wei || typ == mCard.comboTypes.chouwei) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    total += 4;
                } else {
                    total += 3;
                }
            } else if (typ == mCard.comboTypes.dui) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    total += 2;
                } else {
                    total += 0;
                }
            }
            total += xi;
            for (var j = 0; j < comboInfo.cards.length; j++) {
                if (cardNumArray[comboInfo.cards[j]]) {
                    cardNumArray[comboInfo.cards[j]]++;
                } else {
                    cardNumArray[comboInfo.cards[j]] = 1;
                }
            }
        }
        //团圆
        var hasWeiPeng = function (card) {
            for (var i = 0; i < comboList.length; i++) {
                var comboInfo = comboList[i];
                if ((comboInfo.typ == mCard.comboTypes.chouwei || comboInfo.typ == mCard.comboTypes.peng) &&
                    comboInfo.cards.indexOf(card)) {
                    return true;
                }
            }
            return false;
        };
        var hasYiJuHua = function (card) {
            for (var i = 0; i < comboList.length; i++) {
                var comboInfo = comboList[i];
                if (comboInfo.typ == mCard.comboTypes.o234 &&
                    comboInfo.cards.indexOf(card)) {
                    return true;
                }
            }
            return false;
        };
        for (var card in cardNumArray) {
            if (cardNumArray[card] == 4 && hasYiJuHua(card) && hasWeiPeng(card)) {
                total += ([2, 7, 10, 12, 17, 20].indexOf(parseInt(card)) >= 0) ? 4 : 3;
            }
        }
        return total;
    },
    /**
     * 团圆 碰(偎)+一句话  3 4
     * 转弯 将+一句话     2 1
     * @param comboInfo
     * @param comboList
     * @returns {number}
     */
    getComboHuXiWeiMaQue: function (comboInfo, comboList) {
        // console.log(comboInfo);
        // console.log(comboList);
        var cardNumArray = [];
        if (comboList) {
            for (var i = 0; i < comboList.length; i++) {
                var combo = comboList[i];
                for (var j = 0; j < combo.cards.length; j++) {
                    if (cardNumArray[combo.cards[j]]) {
                        cardNumArray[combo.cards[j]]++;
                    } else {
                        cardNumArray[combo.cards[j]] = 1;
                    }
                }
            }
        }

        var xi = 0;
        var typ = comboInfo.typ;
        if (typ == mCard.comboTypes.peng ||
            typ == mCard.comboTypes.wei || typ == mCard.comboTypes.chouwei || typ == mCard.comboTypes.kan) {
            if (typ == mCard.comboTypes.peng) {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    xi = 3;
                } else {
                    xi = 2;
                }
            } else {
                if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                    xi = 4;
                } else {
                    xi = 3;
                }
            }
            //团圆 碰(偎)+一句话  3 4
            for (var card in cardNumArray) {
                if (cardNumArray[card] == 4 && comboInfo.cards[0] == card) {
                    xi += ([2, 7, 10, 12, 17, 20].indexOf(parseInt(card)) >= 0) ? 4 : 3;
                    break;
                }
            }
        } else if (typ == mCard.comboTypes.dui) {
            if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                xi = 2;
            } else {
                xi = 0;
            }
            //转弯 将+一句话     2 3
            if (comboList) {
                for (var j = 0; j < comboList.length; j++) {
                    var combo = comboList[j];
                    if (combo.typ == mCard.comboTypes.o234 || combo.typ == mCard.comboTypes.o123 ||
                        combo.typ == mCard.comboTypes.o2710
                        || combo.typ == mCard.comboTypes.chi) {
                        if (combo.cards.indexOf(comboInfo.cards[0]) >= 0) {
                            if ([2, 7, 10, 12, 17, 20].indexOf(comboInfo.cards[0]) >= 0) {
                                xi += 1;
                            } else {
                                xi += 2;
                            }
                            break;
                        }
                    }
                }
            }
        }
        return xi;
    },
    /**
     *获取组合胡息数
     * @param comboInfo
     * @returns {number}
     */
    getComboHuXi: function (comboInfo) {
        var xi = 0;
        var typ = comboInfo.typ;

        if (comboInfo.cards[0] > 10) {
            xi = mCard.combo_hx_Big[typ - 1];
        } else {
            xi = mCard.combo_hx_Small[typ - 1];
        }
        return xi;
    },
    /**
     * 组合胡息类型
     * @param typ
     * @param isBig
     * @returns {number}
     */
    getComboHuXiByType: function (typ, isBig) {
        var xi = 0;
        if (isBig) {
            xi = mCard.combo_hx_Big[typ - 1];
        } else {
            xi = mCard.combo_hx_Small[typ - 1];
        }
        return xi;
    },
    /**
     * 处理吃碰跑啸倾的手牌显示
     * @param oldCards
     * @param newCards
     * @param targetCardId
     * @param op
     */
    getCardListByDel: function(oldCards, newCards, targetCardId, op){
        var tmpOldCards = deepCopy(oldCards);
        var tmpNewCards = deepCopy(newCards);
        var tmpOldCards2 = [];
        var tmpOldPos =[];
        var tmpOldValid = [];
        var retCards = deepCopy(oldCards);

        function getOldCardsArr() {
            for (var i = 0; i < tmpOldCards.length; i++){
                for (var j = 0; j < tmpOldCards[i].length; j++) {
                    tmpOldCards2.push(tmpOldCards[i][j]);
                    tmpOldPos.push([i, j]);
                    tmpOldValid.push(false);
                }
            }
        }

        function handleCardsValid(cardId, initLocation) {
            var tmpLocation = tmpOldCards2.indexOf(cardId, initLocation);
            if (tmpLocation < 0) return;
            else {
                if (tmpOldValid[tmpLocation]) {
                    handleCardsValid(cardId, tmpLocation + 1);}
                else{
                    tmpOldValid[tmpLocation] = true;
                    return;
                }
            }
        }
        
        function arrFat(arr1, deep) {
            var retArr = [];
            if (deep == 1) {
                for (var i = 0; i < arr1.length; i++) {
                    retArr.push(arr1[i]);
                }
            }
            if (deep == 2){
                for (var i = 0; i < arr1.length; i++) {
                    for (var j = 0; j < arr1[i].length; j++) {
                        retArr.push(arr1[i][j]);
                    }
                }
            }

            return retArr;
        }

        //检测处理后的牌是否和newCards牌一致
        function checkCardsValid(modifyCards) {
            //检测牌是否合法
            var tmpArr1 = deepCopy(modifyCards);
            var tmpArr2 = deepCopy(newCards);
            tmpArr1 = arrFat(tmpArr1, 2);
            tmpArr1.sort(function(a, b){ return a - b;});
            tmpArr2 = arrFat(tmpArr1, 1);
            tmpArr2.sort(function(a, b){ return a - b;});
            if (tmpArr1.length == tmpArr2.length) {
                for (var i = 0; i < tmpArr1.length; i++){
                    if (tmpArr1[i] != tmpArr2[i]) {
                        return false;
                    }
                }
            }
            else {
                return false;
            }

            return true;
        }

        //吃
        if (op == 1 || op == 2 || op == 4)
        {
            //一定在手牌
            getOldCardsArr();
            //for (var i = tmpOldCards.length - 1; i > -1; i--){
            //检索新的队列是否在旧的队列中出现
            for (var i = 0; i < tmpNewCards.length; i++){
                handleCardsValid(tmpNewCards[i], 0);
            }

            //找到未使用的牌
            var notvalidCardLocationArr = [];
            for (var i = 0; i < tmpOldValid.length; i++){
                if (tmpOldValid[i] == false) notvalidCardLocationArr.push(i);
            }

            //删除对应的元素
            //吃牌、碰、啸
            if ((op == 1 || op == 2 || op == 4) && tmpOldCards2.length - newCards.length == 2 &&
                notvalidCardLocationArr.length == 2){
                if (tmpOldPos[notvalidCardLocationArr[0]][0] == tmpOldPos[notvalidCardLocationArr[1]][0]) {
                    var tmpPos1 = tmpOldPos[notvalidCardLocationArr[0]];
                    var tmpPos2 = tmpOldPos[notvalidCardLocationArr[1]];
                    var tmpPosx = retCards[tmpPos1[0]];
                    if (tmpPos1[1] > tmpPos1[1]) {
                        tmpPosx.splice(tmpPos1[1], 1);
                        tmpPosx.splice(tmpPos2[1], 1);
                    }
                    else {
                        tmpPosx.splice(tmpPos2[1], 1);
                        tmpPosx.splice(tmpPos1[1], 1);
                    }
                }
                else {
                    for (var i = 0; i < 2; i++) {
                        var tmpPos = tmpOldPos[notvalidCardLocationArr[i]];
                        var tmpPosx = retCards[tmpPos[0]];
                        tmpPosx.splice(tmpPos[1], 1);
                    }
                }
                //删除列中长度为0的列
                var tempArrLoction = [];
                for (var i = 0; i < retCards.length; i ++) {
                    if (retCards[i].length == 0) tempArrLoction.push(i);
                }
                if (tempArrLoction.length == 1 || tempArrLoction.length == 2) {
                    if (tempArrLoction.length == 1){
                        if (retCards[tempArrLoction[0]].length == 0) retCards.splice(tempArrLoction[0], 1);
                    } else {
                        if (tempArrLoction[0] > tempArrLoction[1]) {
                            retCards.splice(tempArrLoction[0], 1);
                            retCards.splice(tempArrLoction[1], 1);
                        }
                        else {
                            retCards.splice(tempArrLoction[1], 1);
                            retCards.splice(tempArrLoction[0], 1);
                        }
                    }
                }
                else{
                    if (checkCardsValid(retCards)) {
                        return retCards;
                    }
                    else {
                        return this.getCardList(tmpNewCards);
                    }
               }

                if (checkCardsValid(retCards)) {
                    return retCards;
                }
                else {
                    return this.getCardList(tmpNewCards);
                }
            }
            //下火
            else if (op == 1 && ((tmpOldCards2.length - newCards.length == 5 && notvalidCardLocationArr.length == 5) ||
                (tmpOldCards2.length - newCards.length == 8 && notvalidCardLocationArr.length == 8))){
                //未使用的牌的位置
                var locationSt = [];
                for (var i = 0; i < retCards.length; i++) {
                    locationSt[i] = [];
                }

                for (var i = 0; i < notvalidCardLocationArr.length; i++) {
                    var tmpPos = tmpOldPos[notvalidCardLocationArr[i]];
                    //var tmpLen = locationSt[tmpPos[0]].length;
                    locationSt[tmpPos[0]].push(tmpPos[1]);
                    //未使用牌的对应位置
                    /*
                    if (tmpLen == 0) {
                        locationSt[tmpPos[0]].push(tmpPos[1]);
                    } else {
                        for (var j = 0; j < tmpLen; j++) {
                            if (j == tmpLen - 1) {
                                if (locationSt[tmpPos[0]][j] < tmpPos[1]) {
                                    locationSt[tmpPos[0]].splice(j + 1, 0, tmpPos[1]);
                                    break;
                                } else {
                                    locationSt[tmpPos[0]].splice(j, 0, tmpPos[1]);
                                    break;
                                }
                            } else {
                                if (locationSt[tmpPos[0]][j] < tmpPos[1] && locationSt[tmpPos[0]][j + 1] > tmpPos[1]) {
                                    locationSt[tmpPos[0]].splice(j + 1, 0, tmpPos[1]);
                                    break;
                                } else if (j = 0) {
                                    locationSt[tmpPos[0]].splice(0, 0, tmpPos[1]);
                                    break;
                                } else if (j = tmpLen - 1) {
                                    locationSt[tmpPos[0]].splice(j + 1, 0, tmpPos[1]);
                                    break;
                                } else {
                                    console.log("HDX==>nomatching data");
                                    break;
                                }
                            }
                        }
                    }
                     */
                }

                //对应的每个列进行排序
                for (var i = 0; i < locationSt.length; i++) {
                    locationSt[i].sort(function(a, b){return a - b});
                }

                //删除对应列的元素
                for (var i = 0; i < locationSt.length; i++) {
                    for (var j = locationSt[i].length - 1; j > -1; j--) {
                        retCards[i].splice(locationSt[i][j], 1);
                    }
                }

                //删除列中长度为0的列
                var tempArrLoction = [];
                for (var i = 0; i < retCards.length; i ++) {
                    if (retCards[i].length == 0) tempArrLoction.push(i);
                }

                for (var i = tempArrLoction.length - 1; i > -1; i--) {
                    retCards.splice(tempArrLoction[i], 1);
                }

                if (checkCardsValid(retCards)) {
                    return retCards;
                }
                else {
                    return this.getCardList(tmpNewCards);
                }
            }
            else {
                return this.getCardList(tmpNewCards);
            }
        }
        /*
        //碰、啸
        else if (op === 2 || op == 4){
            //一定在手牌
            console.log("HDX==>mismatching 1");
            return this.getCardList(newCards);
        }
        */
        //跑、倾
        else if (op == 3 || op == 5){
            var sameCardIdPos = -1;
            for (var i = 0; i < retCards.length; i++) {
               if (retCards[i].length == 3 && retCards[i][0] == retCards[i][1] && retCards[i][0] == retCards[i][2]) {
                   if (retCards[i][0] == targetCardId) {
                       sameCardIdPos = i;
                       break;
                   }
               }
            }

            if (sameCardIdPos > -1){
                retCards.splice(sameCardIdPos, 1);
                return retCards;
            }
            else {
                var tmpRetCards = [];
                for (var i = 0; i < retCards.length; i++){
                    for (var j = 0; j < retCards[i].length; j++){
                        tmpRetCards.push(retCards[i][j]);
                    }
                }
                if (tmpRetCards.length == newCards.length) {
                    return retCards;
                }
                else {
                    return this.getCardList(newCards);
                }
            }
        }
        else {
            return this.getCardList(newCards);
        }
    },
    /**
     * 计算手牌数据
     * @param cards 手牌数据
     * @returns {Array}
     */
    getCardListWorkCHZ: function (retList, newCardList, isKan) {
        if (isKan) {
            mCard.searchSameCard(retList, newCardList, 4);
            mCard.searchSameCard(retList, newCardList, 3);
        } else {
            mCard.searchSameCard(retList, newCardList, 2);
            mCard.searchJiaoCard(retList, newCardList);
            mCard.search123Card(retList, newCardList, true);
            if (mRoom.wanfatype != mRoom.WEIMAQUE) {
                mCard.search2710Card(retList, newCardList, true);
                mCard.search2710Card(retList, newCardList, false);
            }
            if (mRoom.is1510 == true) {
                mCard.search1510Card(retList, newCardList, true);
                mCard.search1510Card(retList, newCardList, false);
            }
            mCard.search123Card(retList, newCardList);
            //再找   对能不能升级成  交
            if (mRoom.wanfatype != mRoom.WEIMAQUE) mCard.searchDuiToJiao(retList, newCardList);
            mCard.searchLianCard2(retList, newCardList); //大小牌放到一起
            mCard.search123NeedCard(retList, newCardList);
            mCard.searchLianCard(retList, newCardList);//连着的牌

            for (var i = 0; i < newCardList.length; i++) {
                var combo = [];
                if (newCardList[i] == 21) {
                    var isAdded = false;
                    for (var j = 0; j < retList.length; j++) {
                        if (retList[j].length < 3) {
                            retList[j].push(newCardList[i]);
                            isAdded = true;
                            break;
                        }
                    }
                    if (!isAdded) {
                        if (retList.length < 10) {
                            combo.push(newCardList[i]);
                            retList.push(combo);
                        } else {
                            for (var j = retList.length - 1; j > 0; j--) {
                                if (retList[j].length < 3) {
                                    retList[j].push(newCardList[i]);
                                    break;
                                }
                            }
                        }
                    }
                } else {
                    if (retList.length < 10) {
                        combo.push(newCardList[i]);
                        retList.push(combo);
                    } else {
                        for (var j = retList.length - 1; j > 0; j--) {
                            if (retList[j].length < 3) {
                                retList[j].push(newCardList[i]);
                                break;
                            }
                        }
                    }
                }
            }
        }
        var laiziArr = [];
        var twoLian = [];
        var sanLian = [];
        for (var i = 0; i < retList.length; i++) {
            if (retList[i].length == 1 && retList[i][0] == 21) {
                laiziArr.push(i);
            }
        }
        if (laiziArr.length > 0) {
            for (var i = retList.length - 1; i >= 0; i--) {
                if (retList[i] && laiziArr.length > 0) {
                    if (retList[i].length == 2 && retList[i][0] == retList[i][1]) {
                        retList[i][2] = 21;
                        retList.splice(laiziArr[laiziArr.length - 1], 1);
                        laiziArr.splice(laiziArr.length - 1, 1);
                    }
                }
            }
        }
        // if(retList.length > 10) {
        //     var danList = [];
        //     for (var i = 0; i < retList.length; i++) {
        //         if (retList[i].length == 1) {
        //             danList.push({index: i, value: retList[i][0]})
        //         }
        //     }
        //     console.log(danList);
        //     if (danList.length > 0) {
        //         for (var i = 0; i < retList.length - 10; i++) {
        //             var index = danList[i].index;
        //             var value = danList[i].value;
        //         }
        //     }
        //     console.log(retList);
        // }
        return retList;
    },
    // 优先大小放到一块 比如： 6、16
    searchLianCard2: function (list, cards) {
        var deleteList = [];
        for (var i = cards.length - 1; i > 0; i--) {
            for (var j = i - 1; j >= 0; j--) {
                if (cards[i] && cards[j]) {
                    var card2 = cards[i];
                    var card1 = cards[j];
                    if (card2 == 21 || card1 == 21)
                        continue;
                    if (card1 == (card2 - 10)) {
                        var combo = [card2, card1];
                        list.push(combo);
                        deleteList = [];
                        deleteList.push(j);
                        deleteList.push(i);
                        removeInArray(cards, deleteList);
                    }
                }
            }
        }
    },
    getCardList: function (cards) {
        var allCard = [];
        for (var i = 0; i < cards.length; i++) {
            allCard.push(cards[i]);
        }
        allCard.sort(function (a, b) {
            return a - b;
        });
        mCard.allCard = allCard;
        var retList = [];

        if (ZiPaiRoomData.hz_sortId == undefined)
            ZiPaiRoomData.hz_sortId = 2;

        if (gameData.mapId == app.map_id.XIANGXI_ERQISHI) {
            mCard.getCardListWork2710(retList, allCard);
            return retList;
        }
        if (ZiPaiRoomData.hz_sortId == 2) {
            if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) {
                // mCard.getCardListWorkByYZLDS(retList, allCard, true);
                // mCard.getCardListWorkByYZLDS(retList, allCard, false);
                mCard.getCardListWorkCHZ(retList, allCard, true);
                mCard.getCardListWorkCHZ(retList, allCard, false);

            }else {
                //排序全部采用长沙
                mCard.getCardListWork(retList, allCard, true);
                mCard.getCardListWork(retList, allCard, false);
                //排序
                retList.sort(function (a, b) {
                    var isKan = function (kan) {
                        if (kan.length == 3 && kan[0] == kan[1] && kan[0] == kan[2]) return true;
                        return false;
                    };
                    var aquan = 0;
                    var bquan = 0;
                    if (isKan(a)) {
                        aquan = a[0];
                    } else {
                        aquan = 100 + (a[0] + 9) % 10 * 10 + (a[0] + 9) / 10;
                    }
                    if (isKan(b)) {
                        bquan = b[0];
                    } else {
                        bquan = 100 + (b[0] + 9) % 10 * 10 + (b[0] + 9) / 10;
                    }
                    return aquan - bquan;
                });
            }

        } else if (ZiPaiRoomData.hz_sortId == 1) {
            mCard.getCardListWorkLD1(retList, allCard);
        } else {
            retList = mCard.getCardSotrPerfect(retList, allCard);
        }
        //network.sendPhz(5000, 'resetCardType/settingData.hz_sortId/' + ZiPaiRoomData.hz_sortId);
        return retList;
    },
    /**
     * 计算其他玩家手牌数据
     * @param cards
     * @returns {Array}
     */
    getOtherCardList: function (cards) {
        var smallCard = [];
        var bigCard = [];
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] > 10)
                bigCard.push(cards[i]);
            else if (cards[i] < 11)
                smallCard.push(cards[i]);
        }
        smallCard.sort(function (a, b) {
            return a - b;
        });
        bigCard.sort(function (a, b) {
            return a - b;
        });
        var retList = [];
        mCard.getCardListWork2(retList, smallCard, true);
        mCard.getCardListWork2(retList, bigCard, true);
        mCard.getCardListWork2(retList, smallCard, false);
        mCard.getCardListWork2(retList, bigCard, false);
        //排序
        retList.sort(function (a, b) {
            var isKan = function (kan) {
                if (kan.length == 3 && kan[0] == kan[1] && kan[0] == kan[2]) return true;
                return false;
            };
            var aquan = (isKan(a) ? 0 : 100) + a[0];
            var bquan = (isKan(b) ? 0 : 100) + b[0];
            return aquan - bquan;
        });
        return retList;
    },
    /**
     * 手牌凑成门子最多， 门子: 四/三个一样 两个一样 顺子  大小字相同
     * @param retList 返回数组
     * @param newCardList 当前手牌
     * @param isKan 坎 三个一样
     * @returns {*}
     */
    getCardListWork: function (retList, newCardList, isKan) {
        if (isKan) {
            mCard.searchSameCard(retList, newCardList, 4);//找出4张一样的
            mCard.searchSameCard(retList, newCardList, 3);//找出3张一样的
        } else {
            mCard.searchSameCard(retList, newCardList, 2);//找出2张一样的
            mCard.searchJiaoCard(retList, newCardList);//找出交牌
            mCard.search123Card(retList, newCardList, true);//找出真一二三；
            mCard.search2710Card(retList, newCardList, true);//找出大二七十
            mCard.search2710Card(retList, newCardList, false);//找出小二七十
            if (ZiPaiRoomData.options['o1510']) {
                mCard.search1510Card(retList, newCardList, true);//找出大一五十
                mCard.search1510Card(retList, newCardList, false);//找出小一五十
            }
            mCard.search123Card(retList, newCardList);//找出可以成123的牌
            mCard.searchDuiToJiao(retList, newCardList);//对能不能升级成  交
            mCard.search123NeedCard(retList, newCardList);//可以组成顺的牌
            mCard.searchLianCard(retList, newCardList);//连着的牌
            for (var i = 0; i < newCardList.length; i++) {
                var combo = [];
                if (retList.length < 10) {
                    combo.push(newCardList[i]);
                    retList.push(combo);
                } else {
                    for (var j = retList.length - 1; j > 0; j--) {
                        if (retList[j].length < 3) {
                            retList[j].push(newCardList[i]);
                            break;
                        }
                    }
                }
            }
        }
        return retList;
    },
    /**
     * 永州落地扫(最大7个长度)
     * 4个相同、3个相同、同大小写的3张牌的顺子、同数目的大小写的3张牌、2张相同、同大小写的2张牌的顺子、
     * 间隔1个数的相同大小写的2张牌的顺子、间隔1个数的不同同大小写的2张牌的顺子
     */
    getCardListWorkByYZLDS: function (retList, newCardList, isKan) {
        if (isKan) {
            mCard.searchSameCard(retList, newCardList, 4);//找出4张一样的
            mCard.searchSameCard(retList, newCardList, 3);//找出3张一样的
        } else {
            mCard.searchSameCard(retList, newCardList, 2);//找出2张一样的
            mCard.insertJiaoCard(retList, newCardList);//找出绞牌
            mCard.search123Card(retList, newCardList, true);//找出真一二三
            mCard.search2710Card(retList, newCardList, false);//找出小二七十
            mCard.search2710Card(retList, newCardList, true);//找出大二七十
            mCard.search123Card(retList, newCardList);//找出可以成123的牌
            //mCard.searchJiaoCard(retList, newCardList);//找出绞牌
            mCard.searchs1b1LianCard(retList, newCardList);//一大一小 值相同的牌
            mCard.searchsl27LianCard(retList, newCardList);//2 7 或 7 10
            mCard.searchsl210LianCard(retList, newCardList);//2 10
            mCard.search12LianCard(retList, newCardList);//连着的牌
            mCard.search13LianCard(retList, newCardList);//中间孔雀开屏1个可组成3连的牌
            mCard.handle21Location(retList, newCardList);// 处理王的位置

            for (var i = 0; i < newCardList.length; i++) {
                var combo = [];
                if (retList.length < zipai.curBaipaiLength) {
                    combo.push(newCardList[i]);
                    /*
                    if (newCardList.length - 1 > i) {
                        combo.push(newCardList[i]);
                        combo.push(newCardList[i + 1]);
                        i++;
                    }
                    else {
                        combo.push(newCardList[i]);
                    }
                     */
                    retList.push(combo);
                } else {
                    for (var j = retList.length - 1; j > 0; j--) {
                        if (retList[j].length < 3) {
                            retList[j].push(newCardList[i]);
                            break;
                        }
                    }
                }
            }
        }
    },
    /**
     * 手牌凑成门子最多， 门子: 四/三个一样 两个一样 顺子  大小字相同
     * @param retList 返回数组
     * @param newCardList 当前手牌
     * @returns {*}
     */
    getCardListWork2710: function (retList, newCardList) {
        mCard.search123Card(retList, newCardList, true);//找出真一二三；
        mCard.search2710Card(retList, newCardList, true);//找出大二七十
        mCard.search2710Card(retList, newCardList, false);//找出小二七十
        mCard.search123Card(retList, newCardList);//找出可以成123的牌
        mCard.searchSameCard(retList, newCardList, 3);//找出3张一样的
        mCard.search123NeedCard(retList, newCardList);//可以组成顺的牌
        mCard.searchSameCard(retList, newCardList, 2);//找出2张一样的
        for (var i = 0; i < newCardList.length; i++) {
            var combo = [];
            if (retList.length < 10) {
                combo.push(newCardList[i]);
                retList.push(combo);
            } else {
                for (var j = retList.length - 1; j > 0; j--) {
                    if (retList[j].length < 3) {
                        retList[j].push(newCardList[i]);
                        break;
                    }
                }
            }
        }
        return retList;
    },
    /**
     * 其他玩家牌排序
     * @param retList
     * @param newCardList
     * @param isKan
     * @returns {*}
     */
    getCardListWork2: function (retList, newCardList, isKan) {
        if (isKan) {
            mCard.searchSameCard(retList, newCardList, 4);
            mCard.searchSameCard(retList, newCardList, 3);
        } else {
            mCard.search123Card(retList, newCardList, true);
            mCard.searchJiaoCard(retList, newCardList);
            mCard.search2710Card(retList, newCardList, true);
            mCard.search2710Card(retList, newCardList, false);
            if (ZiPaiRoomData.options['o1510']) {
                mCard.search1510Card(retList, newCardList, true);
                mCard.search1510Card(retList, newCardList, false);
            }
            mCard.searchSameCard(retList, newCardList, 2);
            mCard.search123Card(retList, newCardList);
            //再找   对能不能升级成  交
            mCard.searchDuiToJiao(retList, newCardList);
            mCard.search123NeedCard(retList, newCardList);
            mCard.searchLianCard(retList, newCardList);//连着的牌
            for (var i = 0; i < newCardList.length; i++) {
                var combo = [];
                combo.push(newCardList[i]);
                retList.push(combo);
            }
        }
        return retList;
    },
    /**
     * 从小到大排列，没有顺子
     * @param retList
     * @param allCard
     * @param noSort
     */
    getCardListWorkLD1: function (retList, allCard, noSort) {
        //先找出相同的
        var array4 = this.getSame(allCard, 4);
        var array3 = this.getSame(allCard, 3);
        var array2 = this.getSame(allCard, 2);
        this.removeArray(allCard, array4);
        this.removeArray(allCard, array3);
        this.removeArray(allCard, array2);
        //合并到数组
        retList.push.apply(retList, array4);
        retList.push.apply(retList, array3);
        retList.push.apply(retList, array2);
        //剩余
        this.cardListWorkPublic(retList, allCard, true);
    },
    /**
     * 摆牌方式公用
     */
    cardListWorkPublic: function (retList, allCard, isSort) {
        //找出3张连续
        var arrayStreak = [];
        for (var i = 0; i < 7; i++) {
            var arrayTemp = this.get3CertainNumber(allCard, i + 1, i + 2, i + 3);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
            arrayTemp = this.get3CertainNumber(allCard, i + 11, i + 12, i + 13);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
        }
        //再找出2张连续
        for (var i = 0; i < 9; i++) {
            var arrayTemp = this.get2CertainNumber(allCard, i + 1, i + 2);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
            arrayTemp = this.get2CertainNumber(allCard, i + 11, i + 12);
            this.removeArray(allCard, arrayTemp);
            arrayStreak = arrayStreak.concat(arrayTemp);
        }
        //合并到数组，除了纯单牌
        retList.push.apply(retList, arrayStreak);
        for (var i = 0; i < allCard.length; i++) {
            var count = 0;
            for (var j = 0; j < mCard.allCard.length; j++) {
                if (allCard[i] == mCard.allCard[j]) {
                    count++;
                    if (count == 2) {
                        retList.push([allCard[i]]);
                        allCard.splice(i, 1);
                        break;
                    }
                }
            }
        }
        //把数组按首元素大小排序
        if (isSort) {
            retList.sort(function (a, b) {
                return a[0] % 10 - b[0] % 10;
            });
            if (retList[0]) {
                if (retList[0][0] == 20) {
                    retList.push(retList[0]);
                    retList.splice(0, 1);
                }
                if (retList[0][0] == 10) {
                    retList.push(retList[0]);
                    retList.splice(0, 1);
                }
            }
        }
        //不是纯单的牌一起放最后
        var tempArray = [];
        while (allCard.length > 0) {
            tempArray.push(allCard[0]);
            allCard.splice(0, 1);
            if (tempArray.length == 3) {
                retList.push(tempArray);
                tempArray = [];
            }
        }
        if (tempArray.length > 0) retList.push(tempArray);
        //当大于11列时，牌往前放
        if (retList.length > 11) {
            var cardArray = [];
            for (var i = retList.length - 1; i >= 11; i--) {
                cardArray = cardArray.concat(retList[i]);
            }
            for (var i = 10; i >= 0; i--) {
                if (retList[i].length < 3) {
                    retList[i] = retList[i].concat(cardArray.splice(0, 3 - retList[i].length));
                }
                if (cardArray.length == 0) break;
            }
            retList.splice(11, retList.length - 1);
        }
    },
    /**
     * 获取指定3张牌
     */
    get3CertainNumber: function (cards, num1, num2, num3) {
        var array1 = [], array2 = [], array3 = [];
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == num1) array1.push(cards[i]);
            else if (cards[i] == num2) array2.push(cards[i]);
            else if (cards[i] == num3) array3.push(cards[i]);
        }
        var min = Math.min(array1.length, array2.length, array3.length);
        var array = [];
        for (var i = 0; i < min; i++) {
            array.push([array1[i], array2[i], array3[i]]);
        }
        return array;
    },
    /**
     * 获取指定2张牌
     */
    get2CertainNumber: function (cards, num1, num2) {
        var array1 = [], array2 = [];
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == num1) array1.push(cards[i]);
            else if (cards[i] == num2) array2.push(cards[i]);
        }
        var min = Math.min(array1.length, array2.length);
        var array = [];
        for (var i = 0; i < min; i++) {
            array.push([array1[i], array2[i]]);
        }
        return array;
    },
    /**
     * 从排好序的牌中得到指定数量相同的牌
     */
    getSame: function (allCard, sameNum) {
        var presentValue = 0;
        var numCount = 0;
        var array = [];
        for (var i = 0; i < allCard.length; i++) {
            if (presentValue == allCard[i]) {
                numCount++;
                if (i == allCard.length - 1 && numCount == sameNum) {
                    array.push(allCard.slice(i - numCount + 1, i + 1));
                }
            } else {
                if (numCount == sameNum) {
                    array.push(allCard.slice(i - numCount, i));
                }
                presentValue = allCard[i];
                numCount = 1;
            }
        }
        return array;
    },
    /**
     * 数组移除相同的元素
     */
    removeArray: function (array, deleteArray) {
        for (var i = 0; i < deleteArray.length; i++) {
            for (var j = 0; j < deleteArray[i].length; j++) {
                for (var k = 0; k < array.length; k++) {
                    if (deleteArray[i][j] == array[k]) {
                        array.splice(k, 1);
                        break;
                    }
                }
            }
        }
    },
    /**
     * 号称完美的理牌
     * @param retList
     * @param newCardList
     * @returns {*}
     */
    getCardSotrPerfect: function (retList, newCardList) {
        //第一优先级  提(四张)  叠加在一起
        mCard.searchSameCard(retList, newCardList, 4);
        // console.log('第一条执行完:' + JSON.stringify(retList));
        //第二优先级  坎(三张)  叠加在一起
        mCard.searchSameCard(retList, newCardList, 3);
        // console.log('第二条执行完:' + JSON.stringify(retList));
        //第三优先级  两张叠加在一起
        mCard.searchSameCard(retList, newCardList, 2);
        // console.log('第三条执行完:' + JSON.stringify(retList));
        //第四优先级 单张中可以组成一句话的在一起 (优先 1、2、3 --- 2、7、10)
        mCard.search123Card(retList, newCardList, true);   //严格查找 1、2、3的顺子
        mCard.search2710Card(retList, newCardList, true);  //严格查找 2、7、10
        mCard.search2710Card(retList, newCardList, false);
        mCard.search123Card(retList, newCardList); //查找一句话(顺子)
        // console.log('第四条执行完:' + JSON.stringify(retList));
        //第五优先级 姐妹牌 (小1小2 小3小4)
        mCard.searchJieMeiPai(retList, newCardList);
        // console.log('第五执行完:' + JSON.stringify(retList));
        //第六优先级 大小牌 (数字一样  大小不同)
        mCard.searchDaXiaoPai(retList, newCardList, 3);
        mCard.searchDaXiaoPai(retList, newCardList, 2);
        // console.log('第六执行完:' + JSON.stringify(retList));
        //第七优先级 隔辈牌
        mCard.searchGeBeiPai(retList, newCardList, true);
        mCard.searchGeBeiPai2710(retList, newCardList);
        mCard.searchGeBeiPai(retList, newCardList);
        // console.log('第七执行完:' + JSON.stringify(retList));
        //第八条 和 第九条 都在这个方法里处理
        var errorSend = mCard.searchShengYuPai(retList, newCardList);
        // console.log('第八执行完:' + JSON.stringify(retList));
        //最终列排序11
        retList = mCard.searchColumnSort(retList);
        if (errorSend) {
            network.sendPhz(5000, 'lipaiError/123456789/retList:/' + JSON.stringify(retList));
        }
        return retList;
    },
    /**
     * 最终列的排序
     * 除去 提和坎的列 所有列按照最下方数字：壹、一、贰、二 的顺序进行列的排序
     * @param list
     */
    searchColumnSort: function (list) {
        var cardsThree = [];
        var cards = [];
        for (var i = 0; i < list.length; i++) {
            list[i].sort(function (a, b) {
                var tempA = a > 10 ? a - 10 : a;
                var tempB = b > 10 ? b - 10 : b;
                if (tempA == tempB && a > b) {
                    return b - a;
                }
                return tempA - tempB;
            });
            if (list[i].length >= 3 && (list[i][0] == list[i][1] && list[i][0] == list[i][2])) {
                cardsThree.push(list[i]);
            } else {
                cards.push(list[i]);
            }
        }

        for (var i = 0; i < cards.length; i++) {
            for (var j = i + 1; j < cards.length; j++) {
                var cardI = cards[i][0];
                var cardJ = cards[j][0];
                cardI = cardI > 10 ? cardI - 10 : cardI;
                cardJ = cardJ > 10 ? cardJ - 10 : cardJ;
                // 1          1         1             11
                if (cardI == cardJ && cards[i][0] < cards[j][0]) {
                    var temp = cards[i];
                    cards[i] = cards[j];
                    cards[j] = temp;
                } else if (cardI > cardJ) {
                    var temp = cards[i];
                    cards[i] = cards[j];
                    cards[j] = temp;
                }
            }
        }


        // cards.sort(function (cardsA, cardsB) {
        //     var a = cardsA[0];
        //     var b = cardsB[0];
        //     var tempA = a > 10 ? a - 10 : a;
        //     var tempB = b > 10 ? b - 10 : b;
        //     if(tempA == tempB && a > b){
        //         return b - a;
        //     }
        //     return tempA - tempB;
        // });
        return cardsThree.concat(cards);
    },
    /**
     * 第八条 和 第九条 都在这个方法里处理
     * @param list
     * @param cards
     * @returns {boolean}
     */
    searchShengYuPai: function (list, cards) {
        //如果进入到第九条 或者 第八条的cards存在剩余的散排给服务器发送内容
        var errorSend = false;
        //如果现在理出的牌的长度 和上下的散排的长度小于
        //最大上线的长度则直接把散牌单独成为一列
        //第八条 处理剩余牌 所有牌单独成为一列
        var deleteList = [];
        if ((list.length + cards.length) <= 11) {
            for (var i = 0; i < cards.length; i++) {
                var combo = [];
                combo.push(cards[i]);
                deleteList.push(i);
                list.push(combo);
                errorSend = true;
            }
        } else {
            //第九条 如果列数大于指定列数 则散排自动叠加
            //首先组合成壹、一、贰、二 的形式
            cards.sort(function (a, b) {
                var tempA = a > 10 ? a - 10 : a;
                var tempB = b > 10 ? b - 10 : b;
                if (tempA == tempB && a > b) {
                    return b - a;
                }
                return tempA - tempB;
            });
            for (var i = 0; i < cards.length;) {
                var combo = [];
                var two = i + 1;
                var three = i + 2;

                combo.push(cards[i]);
                deleteList.push(i);
                i++;
                //用最笨定的办法 把剩余的散牌 每三个放到一列
                if (cards[two] != undefined) {
                    combo.push(cards[two]);
                    deleteList.push(two);
                    i++;
                }

                if (cards[three] != undefined) {
                    combo.push(cards[three]);
                    deleteList.push(three);
                    i++;
                }
                list.push(combo);
            }
            errorSend = true;
        }
        removeInArray(cards, deleteList);
        return errorSend;
    },
    /**
     * 第六优先级 大小牌 (数字一样  大小不同)
     * @param list
     * @param cards
     * @param num 相同数量
     */
    searchDaXiaoPai: function (list, cards, num) {
        if (num == undefined) {
            console.log('需要传入count');
            return;
        }
        var sameList = {};
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            var cardNumber = card;
            if (cardNumber > 10) cardNumber -= 10;

            if (sameList[cardNumber] == null) {
                sameList[cardNumber] = {};
                sameList[cardNumber].cards = [];
                sameList[cardNumber].pos = [];
                sameList[cardNumber].count = 1;
            } else {
                sameList[cardNumber].count += 1;
            }
            sameList[cardNumber].cards.push(card);
            sameList[cardNumber].pos.push(i);
        }

        var deleteList = [];
        for (var key in sameList) {
            var count = sameList[key].count;
            if (count == num) {
                var combo = sameList[key].cards;
                for (var s = 0; s < combo.length; s++) {
                    if (combo.length == 3 && combo[0] != combo[1]) {
                        var tmp = combo[0];
                        combo[0] = combo[2];
                        combo[2] = tmp;
                    }
                }
                list.push(combo);
                deleteList = deleteList.concat(sameList[key].pos);
            } else if (count == 1) {
                var card = sameList[key].cards[0];
                for (var i = 0; i < list.length; i++) {
                    if (list[i].length == 2) {
                        var listValue = list[i][0];
                        var listValue2 = list[i][1];
                        if (listValue == listValue2) {
                            listValue = listValue > 10 ? listValue - 10 : listValue;
                            card = card > 10 ? card - 10 : card;
                            if (card == listValue) {
                                list[i].push(sameList[key].cards[0]);
                                deleteList = deleteList.concat(sameList[key].pos);
                            }
                        }
                    }
                }
            }

        }
        removeInArray(cards, deleteList);
    },
    /**
     * 第五优先级 姐妹牌 (小1小2 小3小4)
     * @param list
     * @param cards
     */
    searchJieMeiPai: function (list, cards) {
        var deleteList = [];
        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            for (var j = i + 1; j < cards.length; j++) {
                var card2 = cards[j];
                if (card2 == card1 + 1 && card2 != 11) {
                    var isInUse = false;
                    for (var x = 0; x < deleteList.length; x++) {
                        var y = deleteList[x];
                        if (i == y || j == y) {
                            isInUse = true;
                            break;
                        }
                    }

                    if (isInUse == false) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList.push(i);
                        deleteList.push(j);
                    }
                } else if (card2 > card1 + 1) {
                    break;
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 第七优先级 隔辈牌
     * @param list
     * @param cards
     */
    searchGeBeiPai2710: function (list, cards) {
        var deleteList = [];
        //循环所有牌数据
        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            if (card1 != 2 && card1 != 7 && card1 != 12 && card1 != 17) {
                continue;
            }
            for (var j = i + 1; j < cards.length; j++) {
                var card2 = cards[j];
                if (card2 == 7 || card2 == 10 || card2 == 17 || card2 == 20) {
                    var isInUse = false;
                    for (var x = 0; x < deleteList.length; x++) {
                        var y = deleteList[x];
                        if (i == y || j == y) {
                            isInUse = true;
                            break;
                        }
                    }

                    if (isInUse == false) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList.push(i);
                        deleteList.push(j);
                    }
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 第七优先级 隔辈牌
     * @param list
     * @param cards
     * @param is13
     */
    searchGeBeiPai: function (list, cards, is13) {
        var deleteList = [];
        //循环所有牌数据
        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            //如果是强制查找 1 3 这中隔辈牌
            if (is13 == true && card1 != 1 && card1 != 11) {
                continue;
            }

            for (var j = i + 1; j < cards.length; j++) {
                var card2 = cards[j];
                if (card2 == card1 + 2 && card2 != 11 && card2 != 12) {
                    var isInUse = false;
                    for (var x = 0; x < deleteList.length; x++) {
                        var y = deleteList[x];
                        if (i == y || j == y) {
                            isInUse = true;
                            break;
                        }
                    }

                    if (isInUse == false) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList.push(i);
                        deleteList.push(j);
                    }
                } else if (card2 > card1 + 2) {
                    break;
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 获取相同牌的数量
     * @param cards
     * @param card
     * @returns {number}
     */
    getCardCount: function (cards, card) {
        var count = 0;
        for (var i = 0; i < cards.length; i++) {
            if (cards[i] == card) {
                count++;
            }
        }
        return count;
    },
    /**
     * 搜索几张一样牌 添加超过4张王 玩法最多添加7张
     * @param list 返回数据
     * @param cards 手牌
     * @param num 一样的牌数
     */
    searchSameCard: function (list, cards, num) {
        var sameList = {};
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            if (sameList[card] == null) {
                sameList[card] = {};
                sameList[card].count = 1;
                sameList[card].pos = i;
            } else {
                sameList[card].count += 1;
            }
        }

        var deleteList = [];
        for (var key in sameList) {
            var count = sameList[key].count;
            if (count == num || count >= 4) {
                var v = parseInt(key);
                var combo = [];
                var maxNum = count <= 4 ? count : 4;
                for (var i = 0; i < maxNum; i++) {
                    combo.push(v);
                    deleteList.push(sameList[key].pos + i);
                }
                list.push(combo);
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 搜索二七十牌
     * @param list 返回数据
     * @param cards 当前手牌
     * @param isBig 是否是大牌
     */
    search2710Card: function (list, cards, isBig) {
        var list1 = [];
        var list2 = [];
        var list3 = [];

        for (var i = 0; i < cards.length; i++) {
            var cardNumber = cards[i];
            if (isBig == true) {
                cardNumber = cardNumber - 10;
            }

            if (cardNumber == 2) {
                list1.push(i);
            } else if (cardNumber == 7) {
                list2.push(i);
            } else if (cardNumber == 10) {
                list3.push(i);
            }
        }

        var count = Math.min(list1.length, list2.length, list3.length);
        var deleteList = [];
        for (var i = 0; i < count; i++) {
            var combo = [2, 7, 10];
            if (isBig) {
                combo = [12, 17, 20];
            }
            list.push(combo);

            deleteList.push(list1[i]);
            deleteList.push(list2[i]);
            deleteList.push(list3[i]);
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 搜索一五十牌
     * @param list 返回牌数据
     * @param cards 手牌
     * @param isBig 是否大牌
     */
    search1510Card: function (list, cards, isBig) {
        var list1 = [];
        var list2 = [];
        var list3 = [];

        for (var i = 0; i < cards.length; i++) {
            var cardNumber = cards[i];
            if (isBig == true) {
                cardNumber = cardNumber - 10;
            }

            if (cardNumber == 1) {
                list1.push(i);
            } else if (cardNumber == 5) {
                list2.push(i);
            } else if (cardNumber == 10) {
                list3.push(i);
            }
        }

        var count = Math.min(list1.length, list2.length, list3.length);
        var deleteList = [];
        for (var i = 0; i < count; i++) {
            var combo = [1, 5, 10];
            if (isBig) {
                combo = [11, 15, 20];
            }
            list.push(combo);

            deleteList.push(list1[i]);
            deleteList.push(list2[i]);
            deleteList.push(list3[i]);
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 搜索对能升级交的牌
     * @param retList
     * @param list
     */
    searchDuiToJiao: function (retList, list) {
        var deleteList = [];
        for (var i = 0; i < retList.length; i++) {
            if (retList[i].length == 2 && retList[i][0] == retList[i][1]) {
                for (var j = 0; j < list.length; j++) {
                    if (retList[i][0] != 21 && ((list[j] + 10 == retList[i][0]) || (list[j] - 10 == retList[i][0]))) {
                        deleteList.push(j);
                        retList[i].push(list[j]);
                        break;
                    }
                }
            }
        }
        removeInArray(list, deleteList);
    },
    /**
     * 搜索123牌
     * @param list
     * @param cards
     * @param iso123
     */
    search123Card: function (list, cards, iso123) {
        var deleteList = [];
        for (var i = 0; i < cards.length - 2; i++) {
            var card1 = cards[i];

            if (iso123 == true && card1 != 1 && card1 != 11) {
                continue;
            }

            for (var j = i + 1; j < cards.length - 1; j++) {
                var card2 = cards[j];
                if (card2 == card1 + 1) {
                    for (var k = j + 1; k < cards.length; k++) {
                        var card3 = cards[k];
                        if (card3 != 21 &&card3 == card2 + 1 && card3 != 11 && card3 != 12) {
                            var isInUse = false;
                            for (var x = 0; x < deleteList.length; x++) {
                                var y = deleteList[x];
                                if (i == y || j == y || k == y) {
                                    isInUse = true;
                                    break;
                                }
                            }

                            if (isInUse == false) {
                                var combo = [card1, card2, card3];
                                list.push(combo);
                                deleteList.push(i);
                                deleteList.push(j);
                                deleteList.push(k);
                            }
                        } else if (card3 > card2 + 1) {
                            break;
                        }
                    }
                } else if (card2 > card1 + 1) {
                    break;
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 搜索交牌（两大一小，两小一大）
     * @param list
     * @param cards
     */
    searchJiaoCard: function (list, cards) {
        var sameList = {};
        for (var i = 0; i < cards.length; i++) {
            var card = cards[i];
            var cardNumber = card;
            if (cardNumber > 10) cardNumber -= 10;

            if (sameList[cardNumber] == null) {
                sameList[cardNumber] = {};
                sameList[cardNumber].cards = [];
                sameList[cardNumber].pos = [];
                sameList[cardNumber].count = 1;
            } else {
                sameList[cardNumber].count += 1;
            }
            sameList[cardNumber].cards.push(card);
            sameList[cardNumber].pos.push(i);
        }

        var deleteList = [];
        for (var key in sameList) {
            var count = sameList[key].count;
            if (count == 3) {
                var combo = sameList[key].cards;
                for (var s = 0; s < combo.length; s++) {
                    if (combo.length == 3 && combo[0] != combo[1]) {
                        var tmp = combo[0];
                        combo[0] = combo[2];
                        combo[2] = tmp;
                    }
                }
                list.push(combo);
                deleteList = deleteList.concat(sameList[key].pos);
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 查找到对牌并构成绞牌
     * @param list
     * @param cards
     */
    insertJiaoCard:function(list, cards){
        for (var i = 0; i < list.length; i++){
            if (list[i].length == 2){
                var findCard = list[i][0];
                if (findCard < 11){
                    findCard = findCard + 10;
                }
                else if (findCard > 10 && findCard < 21){
                    findCard = findCard - 10;
                }
                else{
                    continue;
                }
                var location = cards.indexOf(findCard);
                if (location < 0) continue;
                else {
                    cards.splice(location, 1);
                    list[i].push(findCard);
                }
            }
        }
    },
    /**
     * 是否是二七十
     * @param card
     * @returns {boolean}
     */
    is2710: function (card) {
        var no = card;
        if (no > 10) no -= 10;
        if (no == 2 || no == 7 || no == 10) {
            return true;
        }
        return false;
    },
    /**
     * 是否是一五十
     * @param card
     * @returns {boolean}
     */
    is1510: function (card) {
        var no = card;
        if (no > 10) no -= 10;
        if (no == 1 || no == 5 || no == 10) {
            return true;
        }
        return false;
    },
    /**
     * 找可以成123的牌
     * @param list
     * @param cards
     */
    search123NeedCard: function (list, cards) {
        var getList = [];
        var deleteList = [];

        for (var i = 0; i < cards.length - 1; i++) {
            var card1 = cards[i];
            var card2 = cards[i + 1];

            if (mCard.is2710(card1) && mCard.is2710(card2)) {
                var combo = [card1, card2];
                getList.push(i);
                list.push(combo);

                deleteList.push(i);
                deleteList.push(i + 1);

                i += 1;
            }
            else {
                if ((card2 == card1 + 1 || card2 == card1 + 2) && (card2 != 11 && card1 != 10)) {
                    var combo = [card1, card2];
                    getList.push(i);
                    list.push(combo);

                    deleteList.push(i);
                    deleteList.push(i + 1);

                    i += 1;
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 两张相连的牌
     */
    searchLianCard: function (list, cards) {
        var deleteList = [];
        for (var i = cards.length - 1; i > 0; i--) {
            for (var j = i - 1; j >= 0; j--) {
                if (cards[i] && cards[j]) {
                    var card2 = cards[i];
                    var card1 = cards[j];
                    if (((card2 == card1 + 1 || card2 == card1 + 2) && card2 != 11 && card1 != 10 && card2 != 21 && card1 != 20) ||
                        (card2 == card1 + 10 && card2 != 21)) {
                        var combo = [card1, card2];
                        list.push(combo);
                        deleteList = [];
                        deleteList.push(j);
                        deleteList.push(i);
                        removeInArray(cards, deleteList);
                    }
                }
            }
        }
    },

    /**
     * 完全相连的牌(不含王)
     */
    search12LianCard: function (list, cards) {
        var deleteList = [];
        var card1;
        var card2;
        for (var i = 0; i < cards.length - 1; i++) {
            card1 = cards[i];
            card2 = cards[i + 1];

            if (card2 == card1 + 1 && card1 != 10 && card1 != 20) {
                var combo = [card1, card2];
                list.push(combo);

                deleteList.push(i);
                deleteList.push(i + 1);

                i += 1;
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 中间空缺开屏1个可组成3连的牌(不含王)
     */
    search13LianCard: function (list, cards) {
        var deleteList = [];
        var card1;
        var card2;

        for (var i = 0; i < cards.length - 1; i++) {
            card1 = cards[i];
            card2 = cards[i + 1];

            if (card2 == card1 + 2 && card1 != 9 && card1 != 10 && card1 != 19 && card1 != 20) {
                var combo = [card1, card2];
                list.push(combo);

                deleteList.push(i);
                deleteList.push(i + 1);

                i += 1;
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 小写、大写值相同的2张牌
     */
    searchs1b1LianCard: function (list, cards) {
        var deleteList = [];
        var card1;
        var card2;
        var combo;
        var cardLenth = cards.length;

        for (var i = 0; i < cardLenth - 1; i++) {
            card1 = cards[i];
            if (card1 > 10) break;
            if (deleteList.indexOf(card1) > -1) continue;

            for (var j = i + 1; j < cardLenth; j++){
                card2 = cards[j];
                if (card2 < 11 || deleteList.indexOf(card2) > -1) continue;
                if (card1 + 10 == card2){
                    combo = [card1, card2];
                    list.push(combo);
                    deleteList.push(i);
                    deleteList.push(j);
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 查找2 7\7 10的将构成270
     */
    searchsl27LianCard: function (list, cards) {
        var deleteList = [];
        var card1;
        var card2;
        var combo;
        var cardLenth = cards.length;

        for (var i = 0; i < cardLenth - 1; i++) {
            card1 = cards[i];
            if (card1 > 17 ) break;
            if (deleteList.indexOf(card1) > -1) continue;

            for (var j = i + 1; j < cardLenth; j++){
                card2 = cards[j];
                if (card2 > 20 || deleteList.indexOf(card2) > -1) continue;
                if ((card1 == 2 && card2 == 7) || (card1 == 7 && card2 == 10) ||
                        (card1 == 12 && card2 == 17) || (card1 == 17 && card2 == 20)){
                    combo = [card1, card2];
                    list.push(combo);
                    deleteList.push(i);
                    deleteList.push(j);
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 查找2 10的将构成270
     */
    searchsl210LianCard: function (list, cards) {
        var deleteList = [];
        var card1;
        var card2;
        var combo;
        var cardLenth = cards.length;

        for (var i = 0; i < cardLenth - 1; i++) {
            card1 = cards[i];
            if (card1 > 17 ) break;
            if (deleteList.indexOf(card1) > -1) continue;

            for (var j = i + 1; j < cardLenth; j++){
                card2 = cards[j];
                if (card2 > 20 || deleteList.indexOf(card2) > -1) continue;
                if ((card1 == 2 && card2 == 10) || (card1 == 12 && card2 == 20)){
                    combo = [card1, card2];
                    list.push(combo);
                    deleteList.push(i);
                    deleteList.push(j);
                }
            }
        }
        removeInArray(cards, deleteList);
    },
    /**
     * 处理的王的位置
     * @param list
     * @param cards
     */
    handle21Location : function(list, cards){
        var saveList = deepCopy(list);
        var saveCards = deepCopy(cards);
        //查到 王的位置 3王 2王 1王
        var wangNum = 0;
        var wangLocation = -1;
        var isList = false;
        //查找list中的王霸
        for (var i = 0; i < list.length; i++){
            if (list[i][0] == 21){
                wangNum = list[i].length;
                wangLocation = i;
                isList = true;
                break;
            }
        }

        //查找cards中的王霸
        if (isList == false) {
            wangLocation = cards.indexOf(21);
            if (wangLocation > -1) wangNum = 1;
        }
        if (wangNum == 0) return;

        //查找对应位置的类型 123, 2 7 10, 绞牌
        var typeArr = [];
        for (var i = 0; i < list.length; i++){
            var tmpCom = list[i];
            if (list[i].length < 3){
                //王霸
                if (isList && i == wangLocation) {
                    typeArr[i] = 21;
                }
                //3个相同
                else if (tmpCom[0] == tmpCom[1] && tmpCom[0] < 10) {
                    typeArr[i] = "xiao3";
                }
                else if (tmpCom[0] == tmpCom[1] && tmpCom[0] > 10 && tmpCom[0] < 21){
                    typeArr[i] = "da3";
                }
                // 123
                else if ((tmpCom[0] == 1 && tmpCom[1] == 2) || (tmpCom[0] == 2 && tmpCom[1] == 3) || (tmpCom[0] == 1 && tmpCom[1] == 3)){
                    typeArr[i] = "xiao123";
                }
                else if ((tmpCom[0] == 11 && tmpCom[1] == 12) || (tmpCom[0] == 12 && tmpCom[1] == 13) || (tmpCom[0] == 11 && tmpCom[1] == 13)){
                    typeArr[i] = "da123";
                }
                // 2 7 10
                else if ((tmpCom[0] == 2 && tmpCom[1] == 7) || (tmpCom[0] == 2 && tmpCom[1] == 10) || (tmpCom[0] == 7 && tmpCom[1] == 10)) {
                    typeArr[i] = "xiao2710";
                }
                else if ((tmpCom[0] == 12 && tmpCom[1] == 17) || (tmpCom[0] == 12 && tmpCom[1] == 20) || (tmpCom[0] == 17 && tmpCom[1] == 20)) {
                    typeArr[i] = "da2710";
                }
                //绞牌
                else if (Math.abs(tmpCom[0] - tmpCom[1]) == 10) {
                    typeArr[i] = "jiaopai";
                }
                //连牌
                else if (Math.abs(tmpCom[0] - tmpCom[1]) == 2 || Math.abs(tmpCom[0] - tmpCom[1]) == 1){
                    typeArr[i] = "lian";
                }
                //其他
                else {
                    typeArr[i] = "other";
                }
            }
            else{
                typeArr[i] = tmpCom.length;
            }
        }



        var tmp21Location = [];
        function findTypeLocation(location, type) {
            if (typeArr.length <= location + 1) return 0;

            for (var i = location + 1; i < typeArr.length; i++){
                if (typeArr[i] == type && list[i].length < 3) {
                    tmp21Location.push(i);
                    list[i].push(21);
                    return i;
                }
            }
            return 0;
        }

        var tmpWangLocation = wangNum;
        //1.放到2个相同的，2.放到123 3.放到2 7 10 上 3. 放到绞牌上 4. 放到1张牌上
        for (var i = 0; i < list.length; i++){
            if (typeArr[i] != 3 && typeArr[i] != 4 && typeArr[i] != 21 && typeArr[i] != "other"){
                if (typeArr[i] == "xiao3" && list[i].length < 3) {
                    var tmpLocation = findTypeLocation(i, "da3");
                    if (tmpLocation == 0){
                        tmp21Location.push(i);
                        list[i].push(21);
                    }
                    else {
                        i--;
                    }

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                       break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "da3" && list[i].length < 3) {
                    list[i].push(21);
                    tmp21Location.push(i);

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "xiao123" && list[i].length < 3){
                    var tmpLocation = findTypeLocation(i, "da123");
                    if (tmpLocation == 0){
                        tmp21Location.push(i);
                        list[i].push(21);
                    }
                    else {
                        i--;
                    }

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "da123" && list[i].length < 3){
                    list[i].push(21);
                    tmp21Location.push(i);

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "xiao2710" && list[i].length < 3){
                    var tmpLocation = findTypeLocation(i, "da2710");
                    if (tmpLocation == 0){
                        list[i].push(21);
                        tmp21Location.push(i);
                    }
                    else {
                        i--;
                    }

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "da2710" && list[i].length < 3){
                    list[i].push(21);
                    tmp21Location.push(i);

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "jiaopai" && list[i].length < 3) {
                    list[i].push(21);
                    tmp21Location.push(i);

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else if (typeArr[i] == "lian" && list[i].length < 3) {
                    list[i].push(21);
                    tmp21Location.push(i);

                    tmpWangLocation = tmpWangLocation -1;
                    if (tmpWangLocation == 0) {
                        break;
                    }
                    else continue;
                }
                else {
                    continue;
                }
            }
        }

        //
        if(tmp21Location.length == 0){
            list = saveList;
            cards = saveCards;
            return;
        }
        tmp21Location.sort();

        //重新排序 1.构成3个相同的放到真相同的后边 2. 放到真123后 3. 放到2 7 10 后 4.放到绞牌后
        //现在放到3个之后
        var modifyArr = [];
        for (var i = 0; i < tmp21Location.length; i++){
            modifyArr.push({type : typeArr[tmp21Location[i]] , val : list[tmp21Location[i]]});
        }

        //删除被修改的牌
        for (var i = tmp21Location.length - 1; i > -1; i--){
            list.splice(tmp21Location[i], 1);
        }

        //删除原有王的列
        if (isList){
            if (tmp21Location.length == wangNum) {
                for (var i = 0; i < list.length; i++) {
                    if (list[i][0] == 21) list.splice(i, 1);
                }
            }
            else if (tmp21Location.length > wangNum){
                list = saveList;
                cards = saveCards;
                return;
            } else {
                for (var i = 0; i < list.length; i++) {
                    if (list[i][0] == 21) list.splice(i, 1);
                }
                //剩于王数放到cards队列
                var surplus = wangNum - tmp21Location.length;
                for (var i = 0; i < surplus; i++){
                    cards.push(21);
                }
            }
        }
        else {
            if (tmp21Location.length == 1) {
                cards.splice(wangLocation, 1);
            }
            else {
                list = saveList;
                cards = saveCards;
                return;
            }
        }



        //返回3个一样位置 返回2个一样的位置
        //最后一张4张相同
        var same4Location = -1;
        //最后一个3张相同
        var same3Location = -1;
        //最后一个顺子
        var like123Location = -1;
        //最后一个2张相同
        var same2Location = -1;
        function findTypeLastArr() {
            for (var i = 0; i < list.length; i++){
                if (list[i].length == 4) {
                    same4Location = i;
                }
                else if(list[i].length == 3){
                    if (list[i][0] == list[i][1]) {
                        same3Location = i;
                    }
                    else {
                        like123Location = i;
                    }
                }
                else if (list[i].length == 2){
                    if (list[i][0] == list[i][1]){
                        same2Location = i;
                    }
                }
            }

            if (same4Location < 0) same4Location = 0;
            else same4Location++;
            if (same3Location < 0) same3Location = same4Location;
            else same3Location++;
            if (same2Location < 0) same2Location = same3Location;
            else same2Location++;
            if (like123Location < 0) like123Location = same2Location;
            else like123Location++;
        }

        findTypeLastArr();

        //添加修改的牌
        for (var i = modifyArr.length - 1; i > -1; i --){
            //list.splice(endLocaltion + tmp21ModifyNum + tmp21Location + 1, 0, modifyArr[i]);
            //放到3连 、2一牌、3一样
            if (modifyArr[i].type == "lian" || modifyArr[i].type == "jiaopai" || modifyArr[i].type == "da2710" || modifyArr[i].type == "xiao2710" ) {
                list.splice(like123Location, 0, modifyArr[i].val);
            }
            //放到2一样 //放到3连
            else if (modifyArr[i].type == "da123" || modifyArr[i].type == "xiao123" ){
                list.splice(same2Location, 0, modifyArr[i].val);
            }
            //放到3连
            else if (modifyArr[i].type == "da3" || modifyArr[i].type == "xiao3") {
                list.splice(same3Location, 0, modifyArr[i].val);
            }
            //放到3连 、2一牌、3一样
            else {
                list.splice(same4Location, 0, modifyArr[i].val);
            }

        }

        function arrFat(arr1, arr2) {
            var retArr = [];
            for (var i = 0; i < arr1.length; i++) {
                for (var j = 0; j < arr1[i].length; j++){
                    retArr.push(arr1[i][j]);
                }
            }

            for (var i = 0; i < arr2.length; i++) {
                retArr.push(arr2[i]);
            }

            return retArr;
        }

        //检测牌是否合法
        var tmpArr1 = [];
        var tmpArr2 = [];
        tmpArr1 = arrFat(saveList, saveCards);
        tmpArr1.sort(function(a, b){ return a - b;});
        tmpArr2 = arrFat(list, cards);
        tmpArr2.sort(function(a, b){ return a - b;});
        if (tmpArr1.length == tmpArr2.length) {
            for (var i = 0; i < tmpArr1.length; i++){
                if (tmpArr1[i] != tmpArr2[i]) {
                    list = saveList;
                    cards = saveCards;
                    return;
                }
            }
        }
        else {
            list = saveList;
            cards = saveCards;
            return;
        }
    },
    /**
     * 获取胡牌信息
     * @param huInfo
     * @returns {{}}
     */
    getHuCardCountInfo: function (huInfo) {
        var info = {};
        info.big = 0;
        info.small = 0;
        info.red = 0;

        if (huInfo != null) {
            mCard.getCardCountInComboList(huInfo.openList, info);
            mCard.getCardCountInComboList(huInfo.kanList, info);
            mCard.getCardCountInComboList(huInfo.huList, info);
        }
        return info;
    },
    /**
     * 组合信息
     * @param comboList
     * @param info
     */
    getCardCountInComboList: function (comboList, info) {
        for (var i = 0; i < comboList.length; i++) {
            var comboInfo = comboList[i];
            var cards = comboInfo.cards;
            for (var j = 0; j < cards.length; j++) {
                var card = cards[j];
                if (card > 10) {
                    info.big += 1;
                } else {
                    info.small += 1;
                }
                if (mCard.is2710(card) == true) {
                    info.red += 1;
                }
            }
        }
    }
};